<?php
namespace payClass\pay;

/**
 * 商户配置
 */
class Config
{

	// 当前文件夹名
    public static $folder;
    // 商户号
    public static $merchant     = '1047';
    // 密钥
    public static $encryKey     = 's6bma614772cj3n2';
    // 密钥
    public static $ivKey        = '542mz3tippelogxc';
    // 同步回调（通知）地址
    public static $merUrl;
    // 异步回调（通知）地址
    public static $notifyUrl;
    // public static $notifyUrl = 'https://www.baidu.com';
    // API
    public static $api          = 'https://pay.jinhushiyan.cn/';

	public function __construct()
	{
		self::$folder    = substr(__DIR__, strrpos(__DIR__, '\\') + 1);

		self::$merUrl    = 'http://'.$_SERVER['HTTP_HOST'];

		self::$notifyUrl = self::$merUrl . DIRECTORY_SEPARATOR . 'payClass' . DIRECTORY_SEPARATOR . self::$folder . DIRECTORY_SEPARATOR . 'sycNotice.php';
	}

    /**
     * [自定义加密]
     * @param  array  $data [description]
     * @return [type]       [description]
     */
    public static function getSign($data = array())
    {
        if (!$data) return false;

        $sign = md5(
            self::$encryKey . '|' .
            self::$merchant . '|' .
            $data['money'] . '|' .
            $data['order_sn'] . '|' .
            $data['notify_url'] . '|' .
            self::$ivKey
        );

        return $sign;
    }

    /**
     * [MD5加密]
     * @param  array  $data [description]
     * @return [type]       [description]
     */
	public static function getSignForMd5($data = array())
    {
        if (!$data) return false;
        // ASCII排序
        ksort($data);

        $signStr = urldecode(http_build_query($data));

        $signStr .= '&key=' . self::$key;

        $sign = md5($signStr);

        return strtoupper($sign);
    }

    /**
     * 获取时间戳（精确到毫秒）
     * @return [type] [description]
     */
    public static function getMsectime()
    {
        list($msec, $sec) = explode(' ', microtime());

        $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);

        return $msectime;
    }

    /**
     * 请求
     * @param string $url
     * @param array $post
     * @return mixed
     */
    public static function curl($url, array $post)
    {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);

        curl_setopt($ch, CURLOPT_HEADER, 0);

        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);

        curl_setopt($ch, CURLOPT_TIMEOUT, 3);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        curl_setopt($ch, CURLOPT_POST, true);

        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');

        if (preg_match('/^https:\/\//i', $url)) {

            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // https请求 不验证证书和hosts

            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 不从证书中检查SSL加密算法是否存在

        }

        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

        curl_setopt($ch, CURLOPT_MAXREDIRS, 2);

        curl_setopt($ch, CURLINFO_HEADER_OUT, true);

        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: multipart/form-data']);

        $response = curl_exec($ch);

        $request_info = curl_getinfo($ch);

        curl_close($ch);

        return $response;
    }

	/**
     * [CURL请求]
     * @param  string $url     [description]
     * @param  array  $params  [description]
     * @param  string $method  [description]
     * @param  string $headers [description]
     * @return [type]          [description]
     */
	public static function setHttpRequest($url, $params = array(), $method = 'POST', $headers = '')
	{
        $ci = curl_init();

        curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);

        curl_setopt($ci, CURLOPT_TIMEOUT, 30);

        curl_setopt($ci, CURLOPT_CUSTOMREQUEST, $method);

        curl_setopt($ci, CURLOPT_URL, $url);

        curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);

        curl_setopt($ci, CURLOPT_HEADER, FALSE);

        curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查

        curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在

        if ($headers != '') {

            curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);

        } else {

            curl_setopt($ci, CURLOPT_HTTPHEADER, array('Content-type: application/json'));

        }

        switch (strtoupper($method)) {
            case 'POST':

                curl_setopt($ci, CURLOPT_POST, TRUE);

                curl_setopt($ci, CURLOPT_POSTFIELDS, $params);

                break;

            case 'PUT':

                curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'PUT');

                curl_setopt($ci, CURLOPT_POSTFIELDS, $params);

                break;

            case 'PATCH':

                curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'PATCH');

                curl_setopt($ci, CURLOPT_POSTFIELDS, $params);

                break;

            case 'DELETE':

                curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');

                curl_setopt($ci, CURLOPT_POSTFIELDS, $params);

                break;

            default:

                curl_setopt($ci, CURLOPT_HTTPGET, TRUE);

                break;
        }

        $response = curl_exec($ci);

        $httpCode = curl_getinfo($ci);

        curl_close($ci);

        return $response;
    }
}